package com.dadibadi.core.auth.vo;

import com.alibaba.fastjson2.annotation.JSONField;
import com.venusoft.developer.common.security.IBaseUser;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;


/**
 * @author : Luis
 * @date : 2025-08-15 10:28
 */
@Data
public class UserAccountListVO implements Serializable, IBaseUser<String> {
    @Serial
    private static final long serialVersionUID = -3527042430905029280L;


	@Schema(description = "userid")
	private String userid;

	@Schema(description = "password")
	private String password;

	@Schema(description = "email")
	private String email;

	@Schema(description = "created_at")
	private Date createdAt;


	@Schema(description = "delete_flag")
	private Integer deleteFlag;

	@Schema(description = "token")
	private String token;

	@Schema(description = "role_type")
	private String roleType;

	@Schema(description = "角色列表")
	private List<String> roleSet;


	@Schema(description = "权限列表")
	@Override
	@JSONField(serialize = false)
	public Collection<? extends GrantedAuthority> getAuthorities() {


		List<SimpleGrantedAuthority> list = new ArrayList<>();
		if (this.getRoleSet() != null) {
			list.addAll(this.getRoleSet().stream().map(r -> new SimpleGrantedAuthority("ROLE_" + r)).toList());
		}
		if (this.getPermissionSet() != null) {
			list.addAll(this.getPermissionSet().stream().map(SimpleGrantedAuthority::new).toList());
		}


		return list;
	}
	@Override
	public String getUserId() {
		return this.userid;
	}


	@Override
	public String getUsername() {
		return this.userid;
	}
}
